// package-star/pages/brand-distribute-setting-supply/index.js
const app = getApp();
import { PACKAGE_STAR_PAGE } from '../../../constants/route';
import { DELIVERY_TYPE, DELIVERY_TYPE_VALUE } from '../../../constants/activity';
import { navToAction, navTo, navBack, tipToast, tipSuccess, showLoading, hideLoading } from '../../../common/env';
import { getDistributeSupply, setDistributeSupply } from '../../../api/brd';
let loaded = false;

Page({
    data: {
        DELIVERY_TYPE_VALUE,
        settingInfo: {},
        selectInfo: {
            allList: DELIVERY_TYPE,
            has: {},
        },
        fromStep: false
    },
    onLoad(options) {
        const { fromStep } = options;
        this.setData({ fromStep });
        this._handleGetDetail();
        app.event.addListener('getDistributeSettingSupplyInfo', this._handleGetSettingInfo.bind(this));
    },
    onUnload() {
        app.event.removeListener('getDistributeSettingSupplyInfo');
    },
    async _handleGetDetail() {
        !loaded && showLoading('加载中...');
        const settingInfo = await getDistributeSupply();
        this.setData({ settingInfo });
        this._handleSelected(settingInfo.deliveryModes);
        if (!loaded) hideLoading(); loaded = true;
    },
    _handleGetSettingInfo(settingInfo) {
        this.setData({ settingInfo });
    },
    _handleSelected(list) {
        let { allList, has } = this.data.selectInfo;
        if (list) {
            if (list.length) {
                has.none = false;
                for (var k in allList) {
                    has[k] = list.findIndex(v => v === k) > -1;
                }
            } else {
                has = {
                    none: true
                };
            }
            this.setData({
                'selectInfo.has': has,
            });
        }
    },
    handleNavTo(e) {
        const { pageName, ...urlParams } = e.currentTarget ? e.currentTarget.dataset : e;
        const { settingInfo } = this.data;
        navToAction(PACKAGE_STAR_PAGE[pageName], {
            ...urlParams,
        }, {
            success(res) {
                setTimeout(() => {
                    app.event.trigger('sendDistributeSettingSupplyInfo', settingInfo);
                }, 800);
            },
        });
    },
    handleSelect(e) {
        const { type } = e.currentTarget.dataset;
        let { deliveryModes = [] } = this.data.settingInfo;
        if (type === 'none') {
            deliveryModes = [];
        } else {
            let _index = deliveryModes.indexOf(type);
            if (_index > -1) {
                deliveryModes.splice(_index, 1);
            } else {
                deliveryModes.push(type);
            }
        }
        this.setData({
            'settingInfo.deliveryModes': deliveryModes,
        });
        this._handleSelected(deliveryModes);
    },
    async handleSave() {
        const { fromStep, settingInfo } = this.data;
        const { deliveryModes, warehouseIds, unifiedShippingFee } = settingInfo;
        if (!deliveryModes || !deliveryModes.length) {
            tipToast('请选择配送方式');
            return;
        }
        if (deliveryModes && deliveryModes.indexOf(DELIVERY_TYPE_VALUE.express) > -1 && (unifiedShippingFee === undefined || unifiedShippingFee === null)) {
            tipToast('请设置配送费');
            return;
        }
        if (!warehouseIds || !warehouseIds.length) {
            tipToast('请选择供货仓');
            return;
        }
        showLoading('保存中...');
        const flag = await setDistributeSupply(settingInfo);
        if (flag) {
            hideLoading();
            tipSuccess('保存成功');
            if (fromStep) {
                navTo(PACKAGE_STAR_PAGE['brandDistributeSettingProduct'], {
                    fromStep,
                });
            } else {
                navBack();
            }
        }
    },
})
